package com.xpsoft.oa.dao.statistics.impl;


import java.text.DecimalFormat;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;

import com.xpsoft.core.dao.impl.BaseDaoImpl;
import com.xpsoft.oa.dao.statistics.StaffEntryapplyDao;
import com.xpsoft.oa.model.statistics.StaffEntryapply;

public class StaffEntryapplyDaoImpl extends BaseDaoImpl<StaffEntryapply> implements StaffEntryapplyDao{

	public StaffEntryapplyDaoImpl() {
		super(StaffEntryapply.class);
	}

	@Override
	public String getMaxUserName(String username) {
		String hql = "select username from AppUser where username like "+"'%"+username+"%'"+" order by username desc";
		Query query = getSession().createQuery(hql);
		String maxUserName = (String)query.setFirstResult(0).setMaxResults(1).setFetchSize(1).uniqueResult();
		if(StringUtils.isBlank(maxUserName)){
			maxUserName = username;
		}else{
			DecimalFormat df = new DecimalFormat("0");
			String number = maxUserName.substring(username.length());
			maxUserName = username + df.format(1 + Integer.parseInt(StringUtils.isBlank(number)?"0":number));
		}
		return maxUserName;
	}

}